<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 抚平数组
        // 将多维数组转换为一维数组的过程称为抚平数组


        function flatArrFun(arr, relArr) {
            if (!Array.isArray(arr)) {
                return arr;
            };
            // 设定参数的初始值
            relArr = relArr || [];
            for (var i = 0; i < arr.length; i++) {
                if (Array.isArray(arr[i])) {
                    console.log(arr[i]);
                    flatArrFun(arr[i], relArr)
                } else {
                    relArr.push(arr[i]);
                };
            }
            return relArr;

        };
        var arr = [1, 2, 3, [4, 5, [6, 7, 8]]];
        // 得到结果是:[1,2,3,4,5,6,7,8]
        console.log(flatArrFun(arr));
    </script>
</body>

</html>